package com.kkd.kkdapp.activity;

import java.util.ArrayList;
import java.util.List;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.kkd.kkdapp.R;
import com.kkd.kkdapp.adapter.MyPlanHistoryAdapter;
import com.kkd.kkdapp.base.BaseActivity;
import com.kkd.kkdapp.http.entity.MyPlanData;
import com.kkd.kkdapp.http.entity.MyPlanDataItem;
import com.kkd.kkdapp.http.manager.HttpCallBack;
import com.kkd.kkdapp.http.manager.KkdHttpClient;
import com.kkd.kkdapp.http.manager.KkdHttpMagager;
import com.kkd.kkdapp.utils.AppUtil;
import com.kkd.kkdapp.utils.Time_Date;
import com.kkd.kkdapp.view.KkdXlistView;
import com.kkd.kkdapp.view.KkdXlistView.KKDScrollListener;

/****
 * 
 * @date 2016-03-16
 * 
 * @author mty
 * 
 * @Name 定期历史
 * 
 */
public class MyPlanHistoryActivity extends BaseActivity implements
		OnClickListener, KKDScrollListener {

	private final int CODE_ZERO = 1;
	private final int CODE_OTHER = 2;

	private RelativeLayout condition_img; // 返回
	private long pageNO = 1;
	private long pageSize = 10;
	private long pageCount;
	private boolean REFRESHFLAG = true;
	private List<MyPlanDataItem> mapList = new ArrayList<MyPlanDataItem>();
	private KkdXlistView manage_lv;
	private ListView mListView;
	private ImageView mNullImg;
	private TextView mNullTv;
	private MyPlanHistoryAdapter adapter;
	private String desc;

	private Handler mHandler = new Handler() {
		public void handleMessage(android.os.Message msg) {
			switch (msg.what) {
			case CODE_ZERO:
				MyPlanData data = (MyPlanData) msg.obj;
				if (adapter == null) {
					adapter = new MyPlanHistoryAdapter(
							MyPlanHistoryActivity.this, mapList);
					mListView.setAdapter(adapter);
				}
				long PageSize = data.getPageSize();
				long totalCount = data.getTotalCount();
				if (totalCount % PageSize != 0) {
					pageCount = (totalCount / PageSize) + 1;
				} else {
					pageCount = (totalCount / PageSize);
				}
				if (pageNO > pageCount) {
					manage_lv.stopLoadMore(-1);
					if (REFRESHFLAG) {
						manage_lv.stopLoadMore(1);
					}
				} else {
					manage_lv.setNoMore(false);
					if (pageNO == 1) {
						mapList.clear();
					}
					mapList.addAll(data.getRecords());
					adapter.notifyDataSetChanged();
					if (data.getRecords().size() < pageSize) {
						manage_lv.stopLoadMore(1);
					} else {
						manage_lv.stopLoadMore(0);
					}
				}
				manage_lv.stopRefresh();
				Time_Date time_Date = new Time_Date();
				String time = time_Date.get_Year_Day();
				manage_lv.setRefreshTime(time);
				if (REFRESHFLAG) {
					if (mapList.size() == 0) {
						mNullImg.setImageResource(R.drawable.img_null);
						mNullTv.setText("暂无K计划购买历史");
						mNullImg.setVisibility(View.VISIBLE);
						mNullTv.setVisibility(View.VISIBLE);
					} else {
						mNullImg.setVisibility(View.GONE);
						mNullTv.setVisibility(View.GONE);
					}
				}
				break;
			case CODE_OTHER:
				showToast(desc);
				manage_lv.stopLoadMore(1);
				manage_lv.stopRefresh();
				break;
			}
		};
	};

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_my_plan_history);
		initView();
		setListener();
	}

	/*************
	 * 初始化控件
	 *************/
	public void initView() {
		condition_img = (RelativeLayout) this.findViewById(R.id.condition_img);
		mNullImg = (ImageView) this.findViewById(R.id.id_no_content);
		mNullTv = (TextView) this.findViewById(R.id.id_no_content_text);
		manage_lv = (KkdXlistView) this.findViewById(R.id.manage_lv);
		manage_lv.setKKDScrollListener(this);
		mListView = manage_lv.mListView;
		mListView.setDividerHeight(0);
		manage_lv.startRefresh();
		if (AppUtil.isConnect(this)) {
			requestTenderDetail();
		} else {
			// mXListView.setPullLoadEnable(false);
			mNullImg.setImageResource(R.drawable.img_null_net);
			mNullTv.setText("网络错误，请稍候下拉刷新重试");
			mNullImg.setVisibility(View.VISIBLE);
			mNullTv.setVisibility(View.VISIBLE);
			manage_lv.stopRefresh();
		}
	}

	/*************
	 * 注册监听事件
	 *************/
	public void setListener() {
		condition_img.setOnClickListener(this);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.condition_img:
			finish();
			break;
		}

	}

	/**
	 * 请求接口
	 */
	public void requestTenderDetail() {
		KkdHttpMagager.getInstance().getMyPlanManager("20", pageNO, pageSize,
				MyPlanData.class, new HttpCallBack() {

					@Override
					public void onSuccess(int statusCode, Object responseBody) {
						MyPlanData data = (MyPlanData) responseBody;
						Message mag = new Message();
						mag.obj = data;
						mag.what = CODE_ZERO;
						mHandler.sendMessage(mag);
					}

					@Override
					public void onFailure(int statusCode, String desc) {
						MyPlanHistoryActivity.this.desc = desc;
						mHandler.sendEmptyMessage(CODE_OTHER);
						if (mapList == null || mapList.size() == 0) {
							mNullImg.setImageResource(R.drawable.img_null_net);
							if (statusCode == KkdHttpClient.NO_NET) {
								mNullTv.setText("网络错误，请稍候下拉刷新重试");
							} else {
								mNullTv.setText("服务异常，请稍候下拉刷新重试");
							}
							mNullImg.setVisibility(View.VISIBLE);
							mNullTv.setVisibility(View.VISIBLE);
						}
					}
				});
	}

	@Override
	public void onRefresh() {
		pageNO = 1;
		requestTenderDetail();
		REFRESHFLAG = true;
	}

	@Override
	public void onLoadMore() {
		pageNO++;
		requestTenderDetail();
		REFRESHFLAG = false;
	}
}
